package com.ns.sbs.oauth.acc.controller;

import com.ns.core.annotations.ReqLog;
import com.ns.core.utils.StpUtil;
import com.ns.core.utils.jwt.SystemUtil;
import com.ns.core.utils.sg.ResultError;
import com.ns.core.utils.sg.ResultOk;
import com.ns.core.vo.au.SsoUserVO;
import com.ns.sbs.oauth.acc.bo.PwdBO;
import com.ns.sbs.oauth.acc.service.IAccountService;
import com.ns.sbs.oauth.acc.vo.AccCompleteVO;
import com.ns.sbs.oauth.acc4auth.service.IAccPasswordService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 密码相关操作
 *
 * @author lw
 */
@RestController
@RequestMapping("/sso/password/")
@RequiredArgsConstructor
@CrossOrigin
public class SsoPasswordController {

    private final IAccPasswordService passwordService;

    private final IAccountService adminService;

    /**
     * 指定用户修改自己密码
     *
     * @param bo 参数
     * @since 2024/10/28
     */
    @PostMapping("update")
    @ReqLog(module = "密码模块", name = "改密码")
    public ResultOk updatePassword(@RequestBody PwdBO bo) {
        // 1.校验参数
        if (!StringUtils.hasText(bo.getOldPwd())) {
            return ResultOk.getError("请输入原始密码！");
        }
        if (!StringUtils.hasText(bo.getNewPwd())) {
            return ResultOk.getError("请输入新密码！");
        }
        // 2.转md5
        SsoUserVO curAdmin = StpUtil.getCurUser();
        if (Objects.isNull(curAdmin)) {
            return ResultOk.getError("用户信息获取失败，请稍后重试！");
        }
        // 3.获取用户信息
        AccCompleteVO account = adminService.getAccById(curAdmin.getId(), StpUtil.getSid());
        if (Objects.isNull(account)) {
            return ResultOk.getError("密码变更失败，请稍后重试！");
        }
        // 4.验证
        String oldPwdMd5 = SystemUtil.getPasswordMd5(account.getId(), bo.getOldPwd());
        if (!Objects.equals(oldPwdMd5, account.getPassword2())) {
            return ResultOk.getError("旧密码输入错误");
        }
        // 5.开始改
        return ResultOk.getByLine(passwordService.updatePassword(account.getId(), bo.getNewPwd()));
    }

    /**
     * 重置密码
     *
     * @param id 参数
     */
    @GetMapping("reset")
    @ReqLog(module = "密码模块", name = "重置密码")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public ResultOk resetPwd(Long id) {
        AccCompleteVO accCompleteVO = adminService.getAccById(id, StpUtil.getSid());
        if (Objects.nonNull(accCompleteVO)) {
            if (StringUtils.hasText(accCompleteVO.getPhone())) {
                try {
                    passwordService.updatePassword(id, accCompleteVO.getPhone());
                } catch (Exception e) {
                    throw new ResultError("密码重置失败,请稍后再试！");
                }
            } else {
                throw new ResultError("请先确认手机号，再重置！");
            }
        }
        return ResultOk.getSuccess("密码已重置，新密码为手机号,请知晓！");
    }
}
